﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuService.CommonServices;
using QuanLyGiaoVuService.Utilities;

namespace QuanLyGiaoVuService.CommandServices
{
    public class SinhVienCommandService:ISinhVienCommandService
    {
        private ISinhVienRepository _sinhVienRepository;
        private ILopMonHocRepository _lopMonHocRepository;
        private IThamSoService _thamSoService;
        private IBangDiemCommandService _bangDiemCommandService;

        public SinhVienCommandService(SinhVienRepository sinhVienRepository, IThamSoService thamSoService, ILopMonHocRepository lopMonHocRepository, IBangDiemCommandService bangDiemCommandService)
        {
            _sinhVienRepository = sinhVienRepository;
            _lopMonHocRepository = lopMonHocRepository;
            _thamSoService = thamSoService;
            _bangDiemCommandService = bangDiemCommandService;
        }

        public SinhVienDTO InsertSinhVien(QuanLyGiaoVuDAL.DTO.SinhVienDTO sinhVienDTO)
        {
            return _sinhVienRepository.InsertSinhVien(sinhVienDTO);
        }
        public SinhVienDTO UpdateSinhVien(QuanLyGiaoVuDAL.DTO.SinhVienDTO sinhVienDTO)
        {
            return _sinhVienRepository.UpdateSinhVien(sinhVienDTO);
        }
        public bool DeleteSinhVien(Guid sinhVienId)
        {
            return _sinhVienRepository.DeleteSinhVien(sinhVienId);
        }

        public bool UpdateDKLuanVan(IEnumerable<Domain.ExcelReadDataModel> data)
        {
            int soTinChiToiThieu = _thamSoService.GetInt(ThamSoConstantUtility.SOTINCHITOITHIEU);
            try
            {
                foreach (var datum in data)
                {
                    var sinhVien = _sinhVienRepository.GetSinhVienByMSSV(datum.DataEntryIndex);
                    if (sinhVien == null)
                        sinhVien = _sinhVienRepository.GetSinhVienByMSSV("0" + datum.DataEntryIndex);
                    if (sinhVien != null)
                    {
                        int tongSoTC;
                        if (Int32.TryParse(datum.DataEntryValue.ToString(), out tongSoTC))
                            sinhVien.DuDKLuanVan = tongSoTC >= soTinChiToiThieu;
                        _sinhVienRepository.UpdateSinhVien(sinhVien);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool InsertOrUpdateSinhVienByList(IEnumerable<SinhVienDTO> sinhVienList)
        {
            try
            {
                foreach (SinhVienDTO sinhVien in sinhVienList)
                {
                    var sinhVienCu = _sinhVienRepository.GetSinhVienByMSSV(sinhVien.MaSinhVien);
                    if (sinhVienCu == null)
                    {
                        string MSSV = sinhVien.MaSinhVien;
                        string khoa = "20" + MSSV.Substring(0, 2);
                        int khoaNam;
                        if (Int32.TryParse(khoa, out khoaNam))
                        {
                            var lopSinhVien = _lopMonHocRepository.GetLopSinhVienByKhoaNam(khoaNam);
                            sinhVien.MaLop = lopSinhVien.MaLop;
                            _sinhVienRepository.InsertSinhVien(sinhVien);
                        }
                    }
                    else
                    {
                        sinhVien.ID = sinhVienCu.ID;
                        sinhVien.Email = sinhVienCu.Email;
                        sinhVien.DuDKLuanVan = sinhVienCu.DuDKLuanVan;
                        sinhVien.LopTruong = sinhVienCu.LopTruong;
                        _sinhVienRepository.UpdateSinhVien(sinhVien);
                    }
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool NhapDiem(IEnumerable<Domain.ExcelReadDataModel> data, Guid maHocKy, Guid maMonHoc)
        {
            try
            {
                foreach (var datum in data)
                {
                    var sinhVien = _sinhVienRepository.GetSinhVienByMSSV(datum.DataEntryIndex);
                    if (sinhVien == null)
                        sinhVien = _sinhVienRepository.GetSinhVienByMSSV("0" + datum.DataEntryIndex);
                    if (sinhVien != null)
                    {
                        BangDiemDTO bangDiem = new BangDiemDTO();
                        bangDiem.MaBangDiem = Guid.NewGuid();
                        bangDiem.MaHocKy = maHocKy;
                        bangDiem.MaMonHoc = maMonHoc;
                        bangDiem.SinhVienId = sinhVien.ID;
                        int diemHocPhan;
                        if (Int32.TryParse(datum.DataEntryValue, out diemHocPhan))
                        {
                            bangDiem.DiemHocPhan = diemHocPhan * 100;
                        }
                        else
                        {
                            float diemHocPhan2;
                            if (float.TryParse(datum.DataEntryValue, out diemHocPhan2))
                            {
                                bangDiem.DiemHocPhan = (int)(diemHocPhan2 * 100);
                            }
                            else
                                bangDiem.DiemHocPhan = 0;
                        }
                        _bangDiemCommandService.InsertOrUpdateBangDiem(bangDiem);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
